package com.xxs.controller.front;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.jfinal.aop.Before;
import com.jfinal.kit.JMap;
import com.jfinal.kit.PathKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.xxs.interceptor.FrontInterceptor;

@Before(FrontInterceptor.class)
public class IndexController extends BaseFrontController {
	public void index(){
		String siteCode = getCurrentSiteCode();
    	String path = getRequest().getServletPath();
    	String base = path+"db/"+siteCode+"/front/_files";
    	setAttr("_files", base);
    	Engine engine = new Engine();
    	String webPath = PathKit.getWebRootPath();
    	engine.setBaseTemplatePath(webPath);
    	Template template = engine.getTemplate("db/"+siteCode+"/front/index.html");
    	String res = template.renderToString(JMap.create("_files","xxxxxx"));
    	System.out.println(res);
    	File file = new File("index.html");
    	try {
    		if (!file.exists()) {
    			file.createNewFile();
    		}
			FileWriter fw = new FileWriter(file.getName());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(res);
			bw.close();
			System.out.println("静态化完成");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	engine = Engine.use("index11111");
    	render(siteCode+"/front/index.html");
    }
}
